import * as Pont from 'pont-engine';
import { CodeGenerator, Interface } from 'pont-engine';

export class FileStructures extends Pont.FileStructures {}

export default class MyGenerator extends CodeGenerator {
  getInterfaceContent(inter: Interface) {
    const paramsCode = inter.getParamsCode();
    const bodyParamsCode = inter.getBodyParamsCode();
    const method = inter.method.toUpperCase();
    const requestParams = bodyParamsCode ? `data` : `params`;

    // 替换类为interface，生成参数的接口类型
    const interfaceTransform = paramsCode.replace('class', 'interface');
    // 获取接口类型的名称
    const interfaceName = paramsCode.split(' ')[1];
    return `
      /**
      * @description ${inter.description}
      */

    import Request from '@/request/index';
    import * as defs from '../../baseClass';

    export ${interfaceTransform}

    export async function request(${requestParams}:${interfaceName}) {
      return Request(
        '${inter.path}',
        ${requestParams},
        '${method}'
      );
    }
   `;
  }
}
